<?php
/**
* @link http://framework.org/
* @version 0.7.0
* @copyright Copyright: 2002-2005 Doutromundo
* @author Ferando Bueno
* @access public
* @package core
*/
/**
* Paginator class - Not really sure which of these is valid, so bare with me
* @package core
* @subpackage paginator
*/
class Paginator {
	//all variables are pivate.
	var $previous;
	var $current;
	var $next;
	var $page;
	var $total_pages;
	var $link_arr;
	var $range1;
	var $range2;
	var $num_rows;
	var $first;
	var $last;
	var $first_of;
	var $second_of;
	var $limit;
	var $prev_next;
	var $base_page_num;
	var $extra_page_num;
	var $total_items;
	var $pagename;
	//Constructor for Paginator.  Takes the current page and the number of items
	//in the source data and sets the current page ($this->page) and the total
	//items in the source ($this->total_items).
	function Paginator($page,$num_rows)
	{
		if(!$page)
		{
			$this->page=1;
		} else {
			$this->page=$page;
		}
		$this->num_rows=$num_rows;
		$this->total_items = $this->num_rows;
	}
	//Takes  $limit and sets $this->limit. Calls private mehods
	//setBasePage() and setExtraPage() which use $this->limit.
	function set_Limit($limit=5)
	{
		$this->limit = $limit;
		$this->setBasePage();
		$this->setExtraPage();
	}
	//This method creates a number that setExtraPage() uses to if there are
	//and extra pages after limit has divided the total number of pages.
	function setBasePage()
	{
		$div=$this->num_rows/$this->limit;
		$this->base_page_num=floor($div);
	}
	function setExtraPage()
	{
		$this->extra_page_num=$this->num_rows - ($this->base_page_num*$this->limit);
	}
	//Used in making numbered links.  Sets the number of links behind and
	//ahead of the current page.  For example if there were a possiblity of
	//20 numbered links and this was set to 5 and the current link was 10
	//the result would be this 5 6 7 8 9 10 11 12 13 14 15.

	function set_Links($prev_next=5)
	{
		$this->prev_next = $prev_next;
	}
	//method to get the total items.
	function getTotalItems()
	{
		$this->total_items = $this->num_rows;
		return $this->total_items;
	}
	//method to get the base number to use in queries and such.
	function getRange1()
	{
		$this->range1=($this->limit*$this->page)-$this->limit;
		return $this->range1;
	}
	//method to get the offset.
	function getRange2()
	{
		if($this->page==$this->base_page_num + 1)
		{
			$this->range2=$this->extra_page_num;
		} else { $this->range2=$this->limit;
		}
		return $this->range2;
	}
	//method to get the first of number as in 5 of .
	function getFirstOf()
	{
		$this->first_of=$this->range1 + 1;
		return $this->first_of;
	}
	//method to get the second number in a series as in 5 of 8.
	function getSecondOf()
	{
		if($this->page==$this->base_page_num + 1)
		{
			$this->second_of=$this->range1 + $this->extra_page_num;
		} else { $this->second_of=$this->range1 + $this->limit;
		}
		return $this->second_of;
	}
	//method to get the total number of pages.
	function getTotalPages()
	{
		if($this->extra_page_num)
		{
			$this->total_pages = $this->base_page_num + 1;
		} else {
			$this->total_pages = $this->base_page_num;
		}
		return $this->total_pages;
	}
	//method to get the first link number.
	function getFirst()
	{
		$this->first=1;
		return $this->first;
	}
	//method to get the last link number.
	function getLast()
	{
		if($this->page == $this->total_pages)
		{
			$this->last=0;
		}else { $this->last = $this->total_pages;
		}
		return $this->last;
	}
	function getPrevious()
	{
		if($this->page > 1)
		{
			$this->previous = $this->page - 1;
		}
		return $this->previous;
	}
	//method to get the number of the link previous to the current link.
	function getCurrent()
	{
		$this->current = $this->page;
		return $this->current;
	}
	//method to get the current page name. Is mostly used in links to the next
	//page.
	function getPageName()
	{
		$this->pagename = $_SERVER['PHP_SELF'];;
		return $this->pagename;
	}
	//method to get the number of the link after the current link.
	function getNext()
	{
		$this->getTotalPages();
		if($this->total_pages != $this->page)
		{
			$this->next = $this->page + 1;
		}
		return $this->next;
	}
	//method that returns an array of the numbered links that should be
	//displayed.
	function getLinkArr()
	{
		//gets the top range
		$top = $this->getTotalPages()- $this->getCurrent();
		if($top <= $this->prev_next)
		{
			$top = $top;
			$top_range = $this->getCurrent() + $top;
		} else { $top = $this->prev_next; $top_range = $this->getCurrent() + $top; }

		//gets the bottom range
		$bottom = $this->getCurrent() -1;
		if($bottom <= $this->prev_next)
		{
			$bottom = $bottom;
			$bottom_range = $this->getCurrent() - $bottom;
		} else { $bottom = $this->prev_next; $bottom_range = $this->getCurrent() - $bottom; }

		$j=0;
		foreach(range($bottom_range, $top_range) as $i)
		{
			$this->link_arr[$j] = $i;
			$j++;
		}
		return $this->link_arr;
	}

}//ends Paginator class
/**
* Paginator4Smarty class - This is very messy -
* @author  Ferando Bueno
* @package core
* @subpackage paginator
*/
class Paginator4Smarty extends Paginator {

	var $display = array();

	function display($getString="") {

		$this->display['names_primeira'] 						= "Primeira";
		$this->display['names_anterior']						= "Anterior";
		$this->display['names_proxima'] 						= "Pr&oacute;xima";
		$this->display['names_ultima'] 							= "&Uacute;ltima";
		$this->display['imgs_first'] 							= "<img src='images/paginator/orange_arrow_first_left.gif' border='0' width='7' height='8'>";
		$this->display['imgs_previous'] 						= "<img src='images/paginator/orange_arrow_prev_left.gif' border='0' width='7' height='8'>";
		$this->display['imgs_next'] 							= "<img src='images/paginator/orange_arrow_next_right.gif' border='0' width='7' height='8'>";
		$this->display['imgs_last'] 							= "<img src='images/paginator/orange_arrow_last_right.gif' border='0' width='7' height='8'>";
		$this->display['separator']								= " <font color='#797979'>|</font> ";
		$this->display['css_names']  							= "items";
		$this->display['css_names_pages']  						= "pages";
		$this->display['css_pageON'] 							= "pages_ON";
		$this->display['css_total'] 							= "titleSection";
		if ($getString!=""){
			$getString ="&".$getString;
		}
		if($this->getCurrent()==1) {
			$first = $this->display['imgs_first'] . " <span class='" . $this->display['css_names'] . "'>" . $this->display['names_primeira'] . "</span>" . $this->display['separator'];
		} else {
			$first  = "<a href='" .  $this->getPageName() . "?page=" . $this->getFirst() . "$getString'>" . $this->display['imgs_first'] . "</a> ";
			$first .= "<a href='" .  $this->getPageName() . "?page=" . $this->getFirst() . "$getString' class='". $this->display['css_names'] . "'>" . $this->display['names_primeira'] . "</a>" . $this->display['separator'];
		}
		if($this->getPrevious()) {
			$prev  = "<a href='" .  $this->getPageName() . "?page=" . $this->getPrevious() . "$getString'>" . $this->display['imgs_previous']."</a> ";
			$prev .= "<a href='" .  $this->getPageName() . "?page=" . $this->getPrevious() . "$getString' class='". $this->display['css_names'] . "'>" . $this->display['names_anterior'] ."</a>"  . $this->display['separator'];
		} else {
			$prev = $this->display['imgs_previous'] . " <span class='" . $this->display['css_names'] . "'>" . $this->display['names_anterior'] . "</span>" .  $this->display['separator'];
		}

		if($this->getNext()) {
			$next  = $this->display['separator'] . "<a href='" . $this->getPageName() . "?page=" . $this->getNext() . "$getString' class='". $this->display['css_names'] ."'>" . $this->display['names_proxima'] . "</a> ";
			$next .= "<a href='" . $this->getPageName() . "?page=" . $this->getNext() . "$getString'>" . $this->display['imgs_next'] . "</a> " . $this->display['separator'];

		} else {
			$next =  $this->display['separator'] . " <span class='" . $this->display['css_names'] . "'>" .  $this->display['names_proxima'] . "</span> " .  $this->display['imgs_next'] . $this->display['separator'];
		}

		if($this->getLast()) {
			$last  = "<a href='" . $this->getPageName() . "?page=" . $this->getLast() . "$getString' class='". $this->display['css_names'] . "'>" . $this->display['names_ultima']."</a> ";
			$last .= "<a href='" . $this->getPageName() . "?page=" . $this->getLast() . "$getString'>".$this->display['imgs_last']."</a>";
		} else {
			$last = " <span class='" . $this->display['css_names'] . "'>" . $this->display['names_ultima'] . "</span> " . $this->display['imgs_last'];
		}

		//		return $this->getFirstOf() . " of " .$this->getSecondOf() . " of " . $this->getTotalItems() . " " . $first . " " . $prev . " " . $next . " " . $last;

		return
		"<table width='100%' height='25' bgcolor='#FFFFFF' cellpadding='0' cellspacing='0' border='0'>
			  <tr>
					<td align='left' colspan='3' class='".$this->display['css_total'] ."' width='100%'>
			mostrando ".$this->getFirstOf()."&#151;".$this->getSecondOf()." de um total de ".$this->getTotalItems()."
					</td>
				</tr>
				<tr><td colspan='3' height='5'></td></tr>
			  <tr><td colspan='3' height='1' bgcolor='#CCCCCC'></td></tr>
				<tr><td colspan='3' height='5'></td></tr>
			  <tr>
			    <td align='left' nowrap width='17%'>
					$first
					$prev
					</td>
					<td align='center' nowrap width='66%'>\n"
		. $this->displayPages($getString)."
					</td>
			    <td align='right' nowrap width='17%'>
					$next
					$last
					</td>
				</tr>
				<tr><td colspan='3' height='5'></td></tr>
			</table>\n";

	}

	//outputs a link set like this Previous 1 2 3 4 5 6 Next
	function displayPages($getString="")	{
		$links = $this->getLinkArr();
		$pagenumbers = "";
		if (!in_array(1, $this->getLinkArr())) {
			$pagenumbers .= "<a href='" . $this->getPageName() . "?page=1$getString' class='". $this->display['css_names_pages'] . "'>1</a>" ." <span class='".$this->display['css_names_pages']."'>...</span>";
		}

		foreach($links as $link) {
			if($link == $this->getCurrent()) {
				$pagenumbers .=  " <span class='" . $this->display['css_pageON'] . "'>$link</span> ";
			} else {
				$pagenumbers .= " <a href='" . $this->getPageName() . "?page=$link"."$getString'  class='". $this->display['css_names_pages'] . "'>".$link."</a>";
			}
		}
		if (!in_array((int)$this->base_page_num+1, $this->getLinkArr())) {
			$pagenumbers .= " <span class='".$this->display['css_names_pages']."'>...</span> <a href='" . $this->getPageName() . "?page=".(int)($this->base_page_num + 1)."$getString' class='". $this->display['css_names_pages'] . "'>" . (int)($this->base_page_num + 1) . "</a>";
		}
		return $pagenumbers;
	}
}

?>